package com.framework.bdf4j.sysadm.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.framework.bdf4j.comm.persist.DataScope;
import com.framework.bdf4j.comm.util.TreeUtils;
import com.framework.bdf4j.sysadm.entity.SysDept;
import com.framework.bdf4j.sysadm.entity.SysDeptRelation;
import com.framework.bdf4j.sysadm.entity.SysLog;
import com.framework.bdf4j.sysadm.mapper.SysDeptMapper;
import com.framework.bdf4j.sysadm.service.SysDeptRelationService;
import com.framework.bdf4j.sysadm.service.SysDeptService;
import com.framework.bdf4j.sysadm.vo.Ztree;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;

/**
 * <p>
 * 部门管理 服务实现类
 * </p>
 *
 */
@Service
@AllArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
    private final SysDeptRelationService sysDeptRelationService;

    /**
     * 添加信息部门
     *
     * @param dept
     *            部门
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDept(SysDept dept) {
        this.save(dept);
        sysDeptRelationService.insertDeptRelation(dept);
        return Boolean.TRUE;
    }

    /**
     * 删除部门
     *
     * @param id
     *            部门 ID
     * @return 成功、失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeptById(Integer id) {
        // 级联删除部门
        List<Integer> idList =
            sysDeptRelationService.list(Wrappers.<SysDeptRelation>query().lambda().eq(SysDeptRelation::getAncestor, id))
                .stream().map(SysDeptRelation::getDescendant).collect(Collectors.toList());

        if (CollUtil.isNotEmpty(idList)) {
            this.removeByIds(idList);
        }

        // 删除部门级联关系
        sysDeptRelationService.deleteAllDeptRealtion(id);
        return Boolean.TRUE;
    }

    /**
     * 更新部门
     *
     * @param sysDept
     *            部门信息
     * @return 成功、失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeptById(SysDept sysDept) {
        // 更新部门状态
        this.updateById(sysDept);
        // 更新部门关系
        SysDeptRelation relation = new SysDeptRelation();
        relation.setAncestor(sysDept.getParentId());
        relation.setDescendant(sysDept.getDeptId());
        sysDeptRelationService.updateDeptRealtion(relation);
        return Boolean.TRUE;
    }

    /**
     * 查询全部部门树
     *
     * @return 树
     */
    @Override
    public List<Ztree> selectTree() {
        List<SysDept> deptList = baseMapper.listDataScope(new DataScope());
        List<Ztree> ztrees = TreeUtils.initDeptZtree(deptList, null);
        return ztrees;
    }

    /**
     * 带数据权限的部门查询
     *
     * @param sysDept
     *            部门信息
     * @return 部门信息
     */
    @Override
    public List<SysDept> listDept(SysDept sysDept) {
    	QueryWrapper<SysDept> ew = new QueryWrapper<SysDept>();
    	ew.like(StrUtil.isNotBlank(sysDept.getName()), "name", sysDept.getName())
    	.like(StrUtil.isNotBlank(sysDept.getDeptCode()), "dept_code", sysDept.getDeptCode())
    	.eq("del_flag", "0");
        return baseMapper.listDept(ew, new DataScope());
    }

    @Override
    public IPage<SysDept> listDeptPage(Page page, SysDept sysDept) {
    	QueryWrapper<SysDept> ew = new QueryWrapper<SysDept>();
    	ew.like(StrUtil.isNotBlank(sysDept.getName()), "name", sysDept.getName())
    	.like(StrUtil.isNotBlank(sysDept.getDeptCode()), "dept_code", sysDept.getDeptCode())
    	.eq("del_flag", "0");
        return baseMapper.listDeptPage(page,ew,new DataScope());
    }
}
